<template lang="html">
    <div class="container" v-loading="loading">
        <div class="search">
            <el-form :inline="true" class="demo-form-inline">
                <el-form-item>
                  <el-input class="form_input" v-model="searchText" placeholder="请输入订单编号或客户姓名"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" :disabled="loading" size="small" @click="onSubmit">搜索</el-button>
                  <el-button type="primary" size="small" @click="onAddtion">新增</el-button>
                  <el-button type="danger" :disabled="isDisabled" size="small" @click="onBatch">批量删除</el-button>
                </el-form-item>
            </el-form>
        </div>

        <div class="tables">
          <el-table :data="tableData" @selection-change="handleSelectionChange" :row-class-name="rowStyle" border :header-cell-style="{background:'#f5f5f5',color:'#666'}" style="width: 100%">
                <el-table-column type="selection" width="40"></el-table-column>
                <el-table-column label="主图" align="center" width="120">
                  <template slot-scope="scope">
                     <img class="main_img" :src="scope.row.picUrl" alt="">
                  </template>
                </el-table-column>
                <el-table-column prop="commodityName" label="名称" align="center"></el-table-column>
                <el-table-column prop="price" label="价格（元）" align="center"></el-table-column>
                <el-table-column prop="name" label="状态" align="center">
                  <template slot-scope="scope">
                      <el-switch @change="onSwitch(scope.row)" v-model="scope.row.opState" :active-value="1" :inactive-value="2" active-color="#4196FF" inactive-color="#D7D7D7"></el-switch>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                          <el-button type="primary" size="mini" @click="fEdit(scope.row)">编辑</el-button>
                          <el-button type="danger" size="mini" @click="fDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
          </el-table>
          <pagination @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange" :currentPage="currentPage" :totalPage="totalPage" />
        </div>



    </div>
</template>

<script>
export default {
  data(){
    return {
      loading:false,
      searchText: '',
      currentPage: 1,
      goType: 1,
      totalPage: 0,
      pageSize: 10,
      tableData: [],
      currentData: [],
    }
  },
  computed:{
    isDisabled:function() {
      return this.currentData.length>0?false:true;
    }
  },
  created(){
    this.selectActivityMainCommodityPage();
  },
  methods:{
    selectActivityMainCommodityPage(){
      this.loading = true;
      this.$http.selectActivityMainCommodityPage({
        query: this.searchText,
        pageSize: this.pageSize,
        pageNumber: this.currentPage
      }).then(res=>{
        this.loading = false;
        this.tableData = res.data.rows;
        this.totalPage = res.data.total;
        this.pageSize = res.data.pageSize;
      });
    },
    rowStyle({row,rowIndex}) {
      for(let i=0;i<this.currentData.length;i++){
        if(JSON.stringify(row) == JSON.stringify(this.currentData[i])){
          return 'row_style';
        }
      }
    },
    handleSelectionChange(e){
      this.currentData = [];
      this.currentData = this.currentData.concat(e);
    },
    onBatch(){
      let aId = [];
      this.currentData.forEach(item=>{aId.push(item.id)});
      this.$confirm('您确定要删除这些商品吗？').then(res=>{
        return this.$http.deleteActivityMainCommoditys({ids:aId.join(',')})
      }).then(res=>{
        if(res.data.code == 0){
          this.$message.success('删除成功');
          this.selectActivityMainCommodityPage();
        }else{
          this.$message.error(res.data.message);
        }
      }).catch(e=>0);
    },
    fDelete(e){
      this.$confirm('您确定要删除此商品吗？').then(res=>{
         this.$http.deleteActivityMainCommodity({id:e.id}).then(res=>{
             if(res.data.code == 0){
               this.$message.success('删除成功');
               this.selectActivityMainCommodityPage();
             }else{
               this.$message.error(res.data.message);
             }
         })
      }).catch(e=>0);
    },
    onSwitch(e){
      this.$http.closeActivityMainCommodity({id: e.id,opState:e.opState}).then(res=>{
        if(res.data.code===0){
            this.$message.success(res.data.message);
        }else{
            this.$message.error(res.data.message);
        }
      })
    },
    onSubmit(){
      this.handleCurrentChange(1);
    },
    handleSizeChange(e){
      this.pageSize = e;
      this.handleCurrentChange(1);
    },
    handleCurrentChange(e){
      this.currentPage = e;
      this.selectActivityMainCommodityPage();
    },
    onAddtion(){
      this.goType = 1;
      this.$router.push({ path: '/activity/activity/add_main_goods'})
    },
    fEdit(e){
      this.goType = 2;
      this.$router.push({ path: '/activity/activity/add_main_goods', query: { id:e.id}})
    },
  },
  beforeRouteLeave(to, from , next){
    if(this.goType == 1 && to.path == '/activity/activity/add_main_goods'){
      to.meta.title = '新增主推商品';
    }else if(this.goType == 2 && to.path == '/activity/activity/add_main_goods'){
      to.meta.title = '编辑主推商品';
    }
    next();
  }

}
</script>

<style lang="css" scoped>
/deep/.el-table .row_style{
  background-color: #f8f8f8 !important;
}
.main_img{
  max-width: 100px;
  display: block;
  margin: 0 auto;
}
</style>
